package com.dm.wallpaper.library.helpers;

import com.dm.wallpaper.library.ResourceTable;
import com.dm.wallpaper.library.abilityPackage.WallpaperBoardApplication;
import com.dm.wallpaper.library.items.Language;
import com.dm.wallpaper.library.preferences.Preferences;
import com.dm.wallpaper.library.utils.Log;
import ohos.aafwk.ability.AbilityPackage;
import ohos.app.Context;
import ohos.global.configuration.Configuration;
import ohos.global.configuration.LocaleProfile;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/*
 * Wallpaper Board
 *
 * Copyright (c) 2017 Dani Mahardhika
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class LocaleHelper {

    public static void setLocale(Locale locale) {
        Locale.setDefault(locale);
        Configuration configuration = WallpaperBoardApplication.getInstance().getResourceManager().getConfiguration();
        configuration.setLocaleProfile(new LocaleProfile(new Locale[]{locale}));

        Log.i("LocaleHelper.setLocale([context, locale]):44   设置语言:" + locale.getDisplayName() + "       " + locale.getCountry());
        ((AbilityPackage)WallpaperBoardApplication.getInstance()).getResourceManager().updateConfiguration(configuration, WallpaperBoardApplication.getInstance().getResourceManager().getDeviceCapability());
    }

    public static Locale getSystem(Context context) {
//        Resources.getSystem().getConfiguration().locale;
//        locale = Resources.getSystem().getConfiguration().getLocales().get(0);
//        return locale;
        return context.getResourceManager().getConfiguration().getFirstLocale();
    }

    public static List<Language> getAvailableLanguages(Context context) {
        List<Language> languages = new ArrayList<>();
        try {
            String[] codes = context.getResourceManager().getElement(ResourceTable.Strarray_languages_code).getStringArray();
            String[] names = context.getResourceManager().getElement(ResourceTable.Strarray_languages_name).getStringArray();

            for (int i = 0; i < names.length; i++) {
                Language language = new Language(names[i], getLocale(codes[i]));
                languages.add(language);
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("LocaleHelper.getAvailableLanguages([context]):83      " + e.getMessage());
        }
        return languages;
    }

    public static Language getCurrentLanguage(Context context) {
        List<Language> languages = getAvailableLanguages(context);
        Locale locale = Preferences.get().getCurrentLocale();

        for (Language language : languages) {
            Locale l = language.getLocale();
            if (locale.toString().equals(l.toString())) {
                return language;
            }
        }
        return new Language("English", new Locale("en", "US"));
    }

    public static Locale getLocale(String language) {
        String[] codes = language.split("_");
        Locale locale;
        if (codes.length == 2) {
            locale = new Locale(codes[0], codes[1]);
        } else {
            locale = Locale.getDefault();
        }
        return locale;
    }
}
